package com.hc.sp04.order.service;

import com.hc.sp04.feign.ItemClient;
import com.hc.sp04.feign.UserClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sp01.pojo.Item;
import sp01.pojo.Order;
import sp01.pojo.User;
import sp01.service.OrderService;
import sp01.util.JsonResult;

import java.util.List;

/**
 * @author He
 * @date 2021/8/30 16:11
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ItemClient itemClient;

    @Autowired
    private UserClient userClient;

    @Override
    public Order getOrder(String orderId) {
        log.info("获取订单，orderId=" + orderId);
        //TODO  :远程调用商品，获取商品列表
        JsonResult<List<Item>> item = itemClient.getItems(orderId);
        //TODO  :远程调用用户，获取用户数据
        JsonResult<User> user = userClient.getUser(8);  //真是项目的话，这个id是不是你获取当前登录用户的id
        Order order = new Order();
        order.setId(orderId);
        order.setUser(user.getData()); //通过远程调用用户服务调用获取用户
        order.setItems(item.getData()); //通过远程调用商品服务获取商品列表
        return order;
    }

    @Override
    public void addOrder(Order order) {
        log.info("添加订单："+order);
        //TODO :远程调用商品，减少商品库存
        //TODO :远程调用用户，增加用户积分

    }
}
